package com.szy.redstonemcu;

import io.netty.buffer.ByteBuf;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class McuMessage implements IMessage {
    public final static int TYPE_ASSEMBLE_CODE=1;
    public final static int TYPE_RESET_RUN=2;
    public final static int TYPE_SAVE_CODE=3;
    public final static int TYPE_READ_CODE=4;

    public final static int TYPE_RETURN_ASSEMBLE_RESULT=5;

    public final static int TYPE_PAUSE=6;

    public final static int TYPE_RESUME=7;

    public static final int TYPE_REQUEST_GUI=8;


    public String code;



    public BlockPos pos;
    public int typeNum;


    public McuMessage() {

    }
    public McuMessage(BlockPos pos, int type){
        this.pos=pos;
        this.typeNum=type;
    }
    public McuMessage(BlockPos pos, int type, String code){
        this.typeNum=type;
        this.code=code;
        this.pos=pos;
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(typeNum);
        buf.writeInt(pos.getX());
        buf.writeInt(pos.getY());
        buf.writeInt(pos.getZ());
        if(typeNum == TYPE_ASSEMBLE_CODE
                || typeNum == McuMessage.TYPE_SAVE_CODE
                || typeNum == McuMessage.TYPE_RETURN_ASSEMBLE_RESULT
                || typeNum== McuMessage.TYPE_READ_CODE){
            buf.writeInt(code.getBytes().length);
            buf.writeBytes(code.getBytes());
        }
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        typeNum=buf.readInt();
        int x=buf.readInt();
        int y=buf.readInt();
        int z=buf.readInt();
        this.pos=new BlockPos(x,y,z);
        if(typeNum == TYPE_ASSEMBLE_CODE
                || typeNum == McuMessage.TYPE_SAVE_CODE
                || typeNum == McuMessage.TYPE_RETURN_ASSEMBLE_RESULT
                || typeNum== McuMessage.TYPE_READ_CODE){
            int len=buf.readInt();
            byte[] data=new byte[len];
            buf.readBytes(data);
            code=new String(data);
        }
    }
}
